<?php

class Sys_LcbApi
{
	function CallServer($url, $data_array)
	{
		$mod_params = new Db_Tables_SysParams();
		$lcb_channelKey = $mod_params->GetVal('lcb_channelKey');
		
		$data_array['channelKey'] = $lcb_channelKey;
		
		$sign = $this->MakeSign($data_array);
		
		$data_array['sign'] = $sign;
		
		$str_array = array();
		
		foreach($data_array as $dkey => $dval)
		{
			$str_array[] = $dkey."=".$dval;
		}
		
		$data_str = implode("&", $str_array);
		
		$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, $url);
    	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    	curl_setopt($ch, CURLOPT_POSTFIELDS, $data_str);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$tmpInfo = curl_exec($ch);
		
    	if (curl_errno($ch)) {
    		echo 'Errno'.curl_error($ch);
    	}
    	curl_close($ch);
		
		$result = Zend_Json::decode($tmpInfo);
		
		return $result;
	}
	
	function CallServerGet($url, $data_array)
	{
		$mod_params = new Db_Tables_SysParams();
		//$lcb_domain = $mod_params->GetVal('lcb_domain');
		$lcb_channelKey = $mod_params->GetVal('lcb_channelKey');
		
		$data_array['channelKey'] = $lcb_channelKey;
		
		$sign = $this->MakeSign($data_array);
		
		$data_array['sign'] = $sign;
		
		$str_array = array();
		
		foreach($data_array as $dkey => $dval)
		{
			$str_array[] = $dkey."=".$dval;
		}
		
		$data_str = implode("&", $str_array);
		
		return $url."?".$data_str;
	}
	
	function MakeSign($data_array)
	{
		usort($data_array, "strcmp");

		$data_str = "[".implode(", ", $data_array)."]huipukey";
		
		$sign = md5(iconv("GBK", "UTF-8", $data_str));
		
		return $sign;
	}
}